package com.example.denglu.pages.Data;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.denglu.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class DataActivity extends AppCompatActivity {

    private ImageView finish;
    private PieChart piechart;
    private int boxsum = 0;
    private int grilsum = 0;
    private Button piebut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        initView();
        getdata();
        gofinish();
        clickgo();
    }

    private void clickgo() {
        piebut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(DataActivity.this, BarActivity.class);
                startActivity(intent);
            }
        });
    }

    private void showdata(int boxsum, int grilsum) {
        ArrayList<PieEntry> pieEntries = new ArrayList<PieEntry>();
        pieEntries.add(new PieEntry(boxsum, "男生"));
        pieEntries.add(new PieEntry(grilsum, "女生"));

        ArrayList<Integer> backcolors = new ArrayList<>();
        backcolors.add(Color.rgb(0, 255, 0));
        backcolors.add(Color.rgb(255, 255, 0));


        PieDataSet pieDataSet = new PieDataSet(pieEntries, "男/女的积分占比");
        pieDataSet.setColors(backcolors);
        pieDataSet.setValueTextSize(18f);
        pieDataSet.setValueFormatter(new PercentFormatter());
        pieDataSet.setValueTextColor(Color.rgb(0, 0, 0));
        PieData pieData = new PieData(pieDataSet);
        piechart.setData(pieData);
        piechart.setDrawCenterText(true); //设置可以绘制中间的文字
//        piechart.setCenterTextColor(Color.BLACK); //中间的文本颜色
//        piechart.setCenterTextSize(40);  //设置中间文本文字的大小
        piechart.setDrawHoleEnabled(false); //绘制中间的圆形
//        piechart.setHoleColor(Color.WHITE);//饼状图中间的圆的绘制颜色
//        piechart.setHoleRadius(40f);//饼状图中间的圆的半径大小
//        piechart.setTransparentCircleColor(Color.BLACK);//设置圆环的颜色
//        piechart.setTransparentCircleAlpha(100);//设置圆环的透明度[0,255]
//        piechart.setTransparentCircleRadius(40f);//设置圆环的半径值
        piechart.setRotationEnabled(false);//设置饼状图是否可以旋转(默认为true)
//        piechart.setRotationAngle(10);//设置饼状图旋转的角度
        piechart.invalidate();

    }

    private void getdata() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, "");
                Request request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/api/park/score/top/list?pageNum=1&pageSize=50")
                        .method("GET", null)
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImJlZmQyYjY2LTRmYzgtNDQzNC1iYWNmLTM0MGFkOWZhODk1OCJ9.A8aRofqoBOYyZNXMn2LSQ4FCPcEyOdCWWxESGpKQFPD0xV_ub7zect0bZ-hIz3SNX3_9zXLye18RxctCAqKagA")
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String string = response.body().string();
                        try {
                            JSONObject jsonObject = new JSONObject(string);
                            int code = jsonObject.getInt("code");
                            if (code == 200) {
                                JSONArray rows = jsonObject.getJSONArray("rows");
                                for (int i = 0; i < rows.length(); i++) {
                                    JSONObject rowsarr = (JSONObject) rows.get(i);
                                    int score = rowsarr.getInt("score");
                                    if (rowsarr.getString("sex").equals("0")) {
                                        boxsum += score;
                                    } else {
                                        grilsum += score;
                                    }
                                }

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        showdata(boxsum, grilsum);
                                    }
                                });
                            }

                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }

                    }
                });
                try {
                    Response response = client.newCall(request).execute();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    private void gofinish() {
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        finish = findViewById(R.id.finish);
        piechart = findViewById(R.id.piechart);
        piebut = (Button) findViewById(R.id.piebut);
    }
}